﻿<div class="listpage-left" style="width:180px;">
    <div class="listpage-left-head ">分类</div>
    <div class="listpage-left-content">
        <ul id="clsBXJGWorkOrderWorkOrder" />
    </div>
</div>
<div class="listpage-right">
    <div id="dgToolbarBXJGWorkOrderWorkOrder" class="datagrid-toolbar1">
        <div class="toolbar_right">
            <!--<input class="easyui-textbox" data-options="width:260, prompt:'名称/助记码/品牌/规格型号/供应商 等...'" />-->
        </div>
    </div>
    <table id="dgBXJGWorkOrderWorkOrder"></table>
</div>
<script>
    (function () {
        //在razor变量两边加单引号会被当成字符串，vs智能提示不会消失
        const m = 'BXJGWorkOrderWorkOrder';
        const canAdd = abp.auth.isGranted('BXJGWorkOrderCreate');
        const canUpdate = abp.auth.isGranted('BXJGWorkOrderUpdate');
        const canDelete = abp.auth.isGranted('BXJGWorkOrderDelete');

        const canConfirme = abp.auth.isGranted('BXJGWorkOrderConfirme');
        const canAllocate = abp.auth.isGranted('BXJGWorkOrderAllocate');
        const canExecute = abp.auth.isGranted('BXJGWorkOrderExecute');
        const canCompletion = abp.auth.isGranted('BXJGWorkOrderCompletion');
        const canReject = abp.auth.isGranted('BXJGWorkOrderReject');

        const $toolbar = $('#dgToolbar' + m);
        const $toolbarRight = $toolbar.find('.toolbar_right');
        const $dg = $('#dg' + m);
        const $cmenu = $('<div />');
        const $cls = $('#cls' + m);

        $cmenu.menu();
        if (canAdd) {
            var $btnAdd = $('<a />').insertBefore($toolbarRight);
            var $btnAddOptions = {
                iconCls: 'icon-add',
                text: abp.localization.bxjgUtils('新增')
            };
            $btnAdd.linkbutton($.extend($btnAddOptions, {
                plain: true,
                onClick: function () {
                    showForm();
                }
            }));
            $cmenu.menu('appendItem', $.extend($btnAddOptions, {
                onclick: function () {
                    $btnAdd.trigger('click');
                }
            }));
        }
        if (canUpdate) {
            var $btnUpdate = $('<a />').insertBefore($toolbarRight);
            var $btnUpdateOptions = {
                iconCls: 'icon-edit',
                text: abp.localization.bxjgUtils('修改')
            };
            $btnUpdate.linkbutton($.extend($btnUpdateOptions, {
                plain: true,
                onClick: function () {
                    var rows = getSelected($(this).text());
                    if (!rows)
                        return;
                    showForm(rows[0].id);
                }
            }));
            $cmenu.menu('appendItem', $.extend($btnUpdateOptions, {
                onclick: function () {
                    $btnUpdate.trigger('click');
                }
            }));
        }
        if (canDelete) {
            var $btnDelete = $('<a />').insertBefore($toolbarRight);
            var $btnDeleteOptions = {
                iconCls: 'icon-remove',
                text: abp.localization.bxjgUtils('删除')
            };
            $btnDelete.linkbutton($.extend($btnDeleteOptions, {
                plain: true,
                onClick: function () {
                    let rows = getSelected($(this).text());
                    if (!rows)
                        return;
                    abp.message.confirm(
                        abp.localization.bxjgUtils('DeleteConfirm'),
                        abp.localization.bxjgUtils('DeleteConfirmPropt'),
                        function (isConfirmed) {
                            if (isConfirmed) {
                                let ary = new Array();
                                $(rows).each(function () {
                                    ary.push(this.id);
                                });
                                abp.ui.setBusy(null, abp.services.bxjgworkorder.workorder.delete({ ids: ary }).done(function (d) {
                                    abp.notify.success(abp.localization.bxjgUtils('删除') + abp.localization.bxjgUtils('成功') + '!');
                                    $btnRefresh.trigger('click');
                                }));
                            }
                        }
                    );
                }
            }));
            $cmenu.menu('appendItem', $.extend($btnDeleteOptions, {
                onclick: function () {
                    $btnDelete.trigger('click');
                }
            }));
        }

        const $btnRefresh = $('<a />').insertBefore($toolbarRight);
        $btnRefresh.linkbutton({
            iconCls: 'icon-reload',
            text: abp.localization.bxjgUtils('刷新'),
            plain: true,
            onClick: function () {
                $dg.datagrid('reload');
            }
        });

        $('-').insertBefore($toolbarRight);


        if (canConfirme) {
            var $btnConfirme = $('<a />').insertBefore($toolbarRight);
            var $btnConfirmeOptions = {
                iconCls: 'icon-audit',
                text: abp.localization.bxjgWorkOrder('确认')
            };
            $btnConfirme.linkbutton($.extend($btnConfirmeOptions, {
                plain: true,
                onClick: function () {
                    var rows = getSelected($(this).text());
                    if (!rows)
                        return;
                    abp.message.error('开发中！');
                }
            }));
            $cmenu.menu('appendItem', $.extend($btnConfirmeOptions, {
                onclick: function () {
                    $btnConfirme.trigger('click');
                }
            }));
        }

        if (canAllocate) {
            var $btnAllocate = $('<a />').insertBefore($toolbarRight);
            var $btnAllocateOptions = {
                iconCls: 'icon-fenpei',
                text: abp.localization.bxjgWorkOrder('分配')
            };
            $btnAllocate.linkbutton($.extend($btnAllocateOptions, {
                plain: true,
                onClick: function () {
                    var rows = getSelected($(this).text());
                    if (!rows)
                        return;
                    abp.message.error('开发中！');
                }
            }));
            $cmenu.menu('appendItem', $.extend($btnAllocateOptions, {
                onclick: function () {
                    $btnAllocate.trigger('click');
                }
            }));
        }

        if (canExecute) {
            var $btnExecute = $('<a />').insertBefore($toolbarRight);
            var $btnExecuteOptions = {
                iconCls: 'icon-banshou',
                text: abp.localization.bxjgWorkOrder('执行')
            };
            $btnExecute.linkbutton($.extend($btnExecuteOptions, {
                plain: true,
                onClick: function () {
                    var rows = getSelected($(this).text());
                    if (!rows)
                        return;
                    abp.message.error('开发中！');
                }
            }));
            $cmenu.menu('appendItem', $.extend($btnExecuteOptions, {
                onclick: function () {
                    $btnExecute.trigger('click');
                }
            }));
        }

        if (canCompletion) {
            var $btnCompletion = $('<a />').insertBefore($toolbarRight);
            var $btnCompletionOptions = {
                iconCls: 'icon-duigou',
                text: abp.localization.bxjgWorkOrder('完成')
            };
            $btnCompletion.linkbutton($.extend($btnCompletionOptions, {
                plain: true,
                onClick: function () {
                    var rows = getSelected($(this).text());
                    if (!rows)
                        return;
                    abp.message.error('开发中！');
                }
            }));
            $cmenu.menu('appendItem', $.extend($btnCompletionOptions, {
                onclick: function () {
                    $btnCompletion.trigger('click');
                }
            }));
        }

        if (canReject) {
            var $btnReject = $('<a />').insertBefore($toolbarRight);
            var $btnRejectOptions = {
                iconCls: 'icon-jujue',
                text: abp.localization.bxjgWorkOrder('拒绝')
            };
            $btnReject.linkbutton($.extend($btnRejectOptions, {
                plain: true,
                onClick: function () {
                    var rows = getSelected($(this).text());
                    if (!rows)
                        return;
                    abp.message.error('开发中！');
                }
            }));
            $cmenu.menu('appendItem', $.extend($btnRejectOptions, {
                onclick: function () {
                    $btnReject.trigger('click');
                }
            }));
        }

        //反操作
        const $fcz = $('<a href="javascript:void(0)" id="mb">更多</a>').insertBefore($toolbarRight);

        const $fczBox = $('<div id="' + m + 'mm" style="width:150px"></div>').appendTo($fcz);

        const $fcz1 = $('<div>反确认</div>').appendTo($fczBox);
        const $fcz2 = $('<div>反分配</div>').appendTo($fczBox);
        const $fcz3 = $('<div>反执行</div>').appendTo($fczBox);
        const $fcz4 = $('<div>反完成</div>').appendTo($fczBox);
        const $fcz5 = $('<div>反拒绝</div>').appendTo($fczBox);

        $fcz.menubutton({
            iconCls1: 'icon-edit',
            menu: '#'+m+'mm'
        });



        const $keywordTextbox = $('<input />').appendTo($toolbarRight);
        $keywordTextbox.textbox({
            width: 150,
            prompt: abp.localization.bxjgWorkOrder('标题/处理人/电话等...'),
            onChange: search
        });

        const $cbox1 = $('<input />').appendTo($toolbarRight);
        $cbox1.combobox({
            editable: false,
            width: 125,
            url: abp.appPath + 'api/services/bxjgworkorder/workOrderCommon/GetAllStatus',
            queryParams: { forType: 1 },
            prompt: '==' + abp.localization.bxjgWorkOrder('BXJG.WorkOrder.WorkOrder.Status') + '==',
            valueField: 'value',
            textField: 'displayText',
            onChange: search
        });
        const $cbox2 = $('<input />').appendTo($toolbarRight);
        $cbox2.combobox({
            editable: false,
            width: 125,
            url: abp.appPath + 'api/services/bxjgworkorder/workOrderCommon/GetAllUrgencyDegree',
            queryParams: { forType: 1 },
            prompt: '==' + abp.localization.bxjgWorkOrder('BXJG.WorkOrder.WorkOrder.UrgencyDegree') + '==',
            valueField: 'value',
            textField: 'displayText',
            onChange: search
        });
        const $cbox3 = $('<input />').appendTo($toolbarRight);
        $cbox3.combogrid({
            method: 'GET',
            editable: true,
            mode: 'remote',
            width: 110,
            idField: 'id',
            textField: 'name',
            panelWidth: 203,//所有列宽+列数*1px+1px
            delay: 500,
            //pagination: true,
            url: abp.appPath + 'api/services/bxjgworkorder/employee/getall',
            queryParams: { forType: 1 },
            prompt: '==' + abp.localization.bxjgWorkOrder('员工') + '==',
            onChange: search,
            columns: [[
                { field: 'name', title: '姓名', width: 120 },
                { field: 'phone', title: '电话', width: 120 }
            ]]
        });

        function search() {
            let data = {};
            let keywords = $keywordTextbox.textbox('getValue');
            if (keywords)
                data.keyword = keywords;
            let status = $cbox1.combobox('getValue');
            if (status)
                data.status = status;
            let urgencyDegree = $cbox2.combobox('getValue');
            if (urgencyDegree)
                data.urgencyDegree = urgencyDegree;

            let employeeId = $cbox3.combogrid('getValue');
            if (employeeId)
                data.employeeId = employeeId;
            $dg.datagrid('load', data);
        }

        $cls.tree({
            url: abp.appPath + 'api/services/bxjgworkorder/WorkOrderCategoryProvider/GetTreeForSelect',
            queryParams: {
                "forType": 1,
                //"ParentId": 1,
                "ParentText": "全部"
            },//?ParentId=21&ForType=1&ParentText=全部分类
            onSelect: function (node) {
                let loadAllParameter = {};
                if (node.id)
                    loadAllParameter.categoryCode = node.code;
                $dg.datagrid('load', loadAllParameter);
            }
        });

        //str:删除 审核，注意本地化
        function getSelected(str) {
            let rows = $dg.datagrid('getSelections');
            if (rows.length == 0) {//若没有选择，返回的是一个空数组，而非undifined/null
                abp.message.warn(abp.localization.bxjgUtils('请选择要{0}的行', $.trim(str)));
                return false;
            }
            return rows;
        }
        //只有新增和修改会使用编辑表单页面，批量审核或其它需要表单的操作应该另外定义方法而不是高度抽象一个表单窗口
        //em：编辑时的id 若不传则新增
        function showForm(id) {
            const $dlg = $('<div id="dlg' + m + '" />');
            if (id)
                $dlg.data('id', id);

            var clsSeclected = $cls.tree('getSelected');
            if (clsSeclected)
                $dlg.data('clsId', clsSeclected.id);

            $dlg.data('onAdded', function () {
                $dlg.dialog('close');
                $dg.datagrid('load');
            });
            $dlg.data('onUpdated', function () {
                $dlg.dialog('close');
                $dg.datagrid('reload');
            });

            $dlg.dialog({
                iconCls: id ? $btnUpdateOptions.iconCls : $btnAddOptions.iconCls,
                title: (id ? $btnUpdateOptions.text : $btnAddOptions.text) + abp.localization.bxjgWorkOrder('BXJGWorkOrder'),
                width: 850,
                height: 600,
                border: false,
                bodyCls: 'dlg-box',
                modal: true,
                href: '/bxjgworkorder/workorder/form.html?_t=' + abp.pageLoadTime.getTime(),
                buttons: [{
                    iconCls: 'icon-ok',
                    text: abp.localization.bxjgUtils('Save'),
                    handler: function () {
                        var callback = $dlg.data('clickSave');
                        if (callback)
                            callback();
                    }
                }, {
                    iconCls: 'icon-undo',
                    text: abp.localization.bxjgUtils('Back'),
                    handler: function () {
                        $dlg.dialog('close');
                    }
                }],
                onClose: function () {
                    $dlg.dialog('destroy');
                }
                //可以通过onload事件初始化控件，如：下拉框
            });
        }

        $dg.datagrid({
            ctrlSelect: true,
            fit: true,
            checkOnSelect: true,
            selectOnCheck: true,
            singleSelect: false,
            border: false,
            showFooter1: true,
            rownumbers: true,
            pagination: true,
            //nowrap: false,
            toolbar: '#dgToolbar' + m,
            //striped:true,
            ///api/services/app/BXJGShopProduct/GetAll
            url: abp.appPath + 'api/services/bxjgworkorder/workorder/GetAll',
            queryParams: {
                //"CategoryId": 1,
                //"Published": true
            },
            frozenColumns: [[
                { field: 'ck', checkbox: true },
                { field: 'title', title: '标题', width: 300, sortable: true }
            ]],
            columns: [[
                { field: 'categoryDisplayName', title: '类别', sortable: true },
                {
                    field: 'statusDisplayName', title: '状态', width: 70, sortable: true, align: 'center', formatter: function (value, row, index) {
                        return '<span class="bxjg-workorder-status bxjg-workorder-status-' + row.status + '">' + value + '</span>';
                    }
                },
                { field: 'statusChangedDescription', width: 100, title: '状态说明' },
                { field: 'statusChangedTime', title: '状态时间', width: 160, sortable: true, align: 'center' },
                {
                    field: 'urgencyDegreeDisplayName', title: '紧急程度', width: 90, sortable: true, align: 'center', formatter: function (value, row, index) {
                        return '<span class="bxjg-workorder-urgency-degree bxjg-workorder-urgency-degree-' + row.urgencyDegree + '">' + value + '</span>';
                    }
                },

                { field: 'employeeName', title: '处理人', width: 80, sortable: true },
                { field: 'employeePhone', title: '处理人电话', width: 120, align: 'center' },

                { field: 'estimatedExecutionTime', title: '预计开始时间', width: 160, sortable: true, align: 'center' },
                { field: 'estimatedCompletionTime', title: '预计结束时间', width: 160, sortable: true, align: 'center' },

                { field: 'executionTime', title: '执行时间', width: 160, sortable: true, align: 'center' },
                { field: 'completionTime', title: '完成时间', width: 160, sortable: true, align: 'center' }
            ]],
            onBeforeLoad: function (param) {

                if (param.sort == 'urgencyDegreeDisplayName') {
                    param.sort = 'urgencyDegree';
                }
                else if (param.sort == 'statusDisplayName') {
                    param.sort = 'status';
                }
                else if (param.sort == 'employeeName') {
                    param.sort = 'employeeId';
                }
                else if (param.sort == 'categoryDisplayName') {
                    param.sort = 'categoryId';
                }

                $.fn.datagrid.defaults.onBeforeLoad(param);
            },
            onRowContextMenu: function (e, index, row) {
                if (!row)
                    return;
                e.preventDefault();//禁止浏览器默认右键菜单
                //可以在这里临时创建右键菜单，则可以通过闭包访问row
                var rows = $dg.datagrid('getSelections');
                if (rows.length == 1)
                    $dg.datagrid('clearSelections')
                $dg.datagrid('selectRow', index);
                $cmenu.menu('show', {
                    left: e.pageX,
                    top: e.pageY
                });
            },
            onDblClickRow: function (index, row) {
                if ($btnUpdate)
                    $btnUpdate.trigger('click');
            }
        });

    }());
</script>